package com.hongye.why.base;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * Activity堆栈
 */
public class ActivityStack {
    private static List<Activity> activities = new ArrayList<>();

    public static void push(Activity activity) {
        activities.add(activity);
    }

    public static void remove(Activity activity) {
        activities.remove(activity);
    }

    public static void finish(Activity activity) {
        activity.finish();
        activities.remove(activity);
    }

    public static Activity topActivity() {
        if (activities.size() > 0) {
            return activities.get(activities.size()-1);
        }
        return null;
    }

    public static void finishAll() {
        for (Activity actvity : activities) {
            actvity.finish();
        }
        activities.clear();
    }

    /**
     * 退出所有Activity除clas之外
     * @param clas Class 想要保留的Activity
     */
    public static void finishAllExceptActivity(Class clas) {
        for (Activity activity : activities) {
            if (activity.getClass() != clas) {
                activity.finish();
            }
        }
    }
}
